package vn.codespot.hms.utils;

import java.util.Locale;
import java.util.Map;

import vn.codespot.VisaVietNamAndroid.R;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.widget.RadioButton;

public class HMSUtils {
    public static final String CLASS_NAME = HMSUtils.class.getName();

    public static boolean isInternetAvailable(Context context) {
        ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connMgr == null) {
            return false;
        }

        if (connMgr.getActiveNetworkInfo() == null) {
            return false;
        }
        return true;
    }

    public static void showErrorDialog(Context context, String errorMessage) {
        String title = context.getString(R.string.titleError);
        showDialog(context, title, errorMessage, android.R.drawable.ic_dialog_alert);
    }

    public static void showDialog(Context context, String title, String message, int icon) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setCancelable(false);
        builder.setIcon(icon);
        builder.setPositiveButton(context.getString(R.string.titleOK), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                dialog.dismiss();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    public static void showExitCofirmDialog(final Context context, final RadioButton rad) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(context.getText(R.string.titleConfirmation));
        builder.setMessage(context.getString(R.string.confirmExit));
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setPositiveButton(context.getString(R.string.titleOK), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                rad.setChecked(true);
                Intent intentExit = new Intent(Intent.ACTION_MAIN);
                intentExit.addCategory(Intent.CATEGORY_HOME);
                intentExit.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intentExit);
            }
        });
        builder.setNegativeButton(context.getString(R.string.titleCancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                rad.setChecked(true);
            }
        });

        AlertDialog dialog = builder.create();
        dialog.show();
    }

    public static boolean isAsyncTaskInProgress(AsyncTask<?, ?, ?> task) {
        if (task != null && !task.isCancelled() && task.getStatus() != AsyncTask.Status.FINISHED) {
            return true;
        }
        return false;
    }

    public static void setLanguage(ContextWrapper contextWraper) {
        String lang = Locale.getDefault().getLanguage();
        if (lang.equals("vi")) {
            lang = "vi";
        } else {
            lang = "en";
        }
        Locale locale = new Locale(lang);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        contextWraper.getBaseContext().getResources()
                .updateConfiguration(config, contextWraper.getBaseContext().getResources().getDisplayMetrics());
    }

    public static ProgressDialog showProcessingDialog(Context context) {
        return ProgressDialog.show(context, context.getString(R.string.titleProcessing), context.getString(R.string.titlePleasewait),
                false, false);
    }

    /**
     * Get HTTP request query as type ?param1=value1& param2=value2
     * 
     * @param parameters
     * @return
     */
    public static String getHttpGetQuery(Map<String, String> parameters) {
        String query = "";
        if (parameters != null && !parameters.isEmpty()) {
            StringBuilder queryBuilder = new StringBuilder();
            queryBuilder.append("?");
            for (String param : parameters.keySet()) {
                String paramValue = parameters.get(param);
                // if (!TextUtils.isEmpty(paramValue)) {
                queryBuilder.append(param);
                queryBuilder.append("=");
                queryBuilder.append(paramValue);
                queryBuilder.append("&");
                // }
            }
            query = queryBuilder.toString();
            if (query.endsWith("&")) {
                query = query.substring(0, query.length() - 1);
            }
        }
        return query;
    }

}
